package com.test.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public abstract class GZipUtils {

    public static final int BUFFER = 1024;
    public static final String EXT = ".gz";

    /**
     * 数据压缩
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] compress(byte[] data) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // 压缩
        compress(bais, baos);

        byte[] output = baos.toByteArray();

        baos.flush();
        baos.close();

        bais.close();

        return output;
    }

    /**
     * 文件压缩
     *
     * @param file
     * @throws Exception
     */
    public static void compress(File file) throws Exception {
        compress(file, true);
    }

    /**
     * 文件压缩
     *
     * @param file
     * @param delete 是否删除原始文件
     * @throws Exception
     */
    public static void compress(File file, boolean delete) throws Exception {
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);

        compress(fis, fos);

        fis.close();
        fos.flush();
        fos.close();

        if (delete) {
            file.delete();
        }
    }

    /**
     * 数据压缩
     *
     * @param is
     * @param os
     * @throws Exception
     */
    public static void compress(InputStream is, OutputStream os) throws Exception {

        GZIPOutputStream gos = new GZIPOutputStream(os);

        int count;
        byte data[] = new byte[BUFFER];
        while ((count = is.read(data, 0, BUFFER)) != -1) {
            gos.write(data, 0, count);
        }

        gos.finish();

        gos.flush();
        gos.close();
    }

    /**
     * 文件压缩
     *
     * @param path
     * @throws Exception
     */
    public static void compress(String path) throws Exception {
        compress(path, true);
    }

    /**
     * 文件压缩
     *
     * @param path
     * @param delete 是否删除原始文件
     * @throws Exception
     */
    public static void compress(String path, boolean delete) throws Exception {
        File file = new File(path);
        compress(file, delete);
    }

    /**
     * 数据解压缩
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] decompress(byte[] data) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // 解压缩

        decompress(bais, baos);

        data = baos.toByteArray();

        baos.flush();
        baos.close();

        bais.close();

        return data;
    }

    /**
     * 文件解压缩
     *
     * @param filepath
     * @param targetFilePath
     * @param delete
     */
    public static void decompress(String filepath, String targetFilePath, boolean delete) {
        File file = new File(filepath);
        decompress(file, delete, targetFilePath);
    }

    /**
     * 文件解压缩
     *
     * @param file
     * @throws Exception
     */
    public static void decompress(File file, String targetFilePath) throws Exception {
        decompress(file, true, targetFilePath);
    }

    /**
     * 文件解压缩
     *
     * @param file
     * @param delete 是否删除原始文件
     * @throws Exception
     */
    public static void decompress(File file, boolean delete, String targetFilePath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file);
            if (StringUtils.isBlank(targetFilePath)) {
                targetFilePath = file.getPath().replace(EXT, "");
            }
            fos = new FileOutputStream(targetFilePath);
            decompress(fis, fos);
            if (delete) {
                file.delete();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            try {
                fos.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 数据解压缩
     *
     * @param is
     * @param os
     * @throws Exception
     */
    public static void decompress(InputStream is, OutputStream os) throws Exception {

        GZIPInputStream gis = new GZIPInputStream(is);

        int count;
        byte data[] = new byte[BUFFER];
        while ((count = gis.read(data, 0, BUFFER)) != -1) {
            os.write(data, 0, count);
        }

        gis.close();
    }

    /**
     * 文件解压缩
     *
     * @param path
     * @throws Exception
     */
    public static void decompress(String path) throws Exception {
        decompress(path, true);
    }

    /**
     * 文件解压缩
     *
     * @param path
     * @param delete 是否删除原始文件
     * @throws Exception
     */
    public static void decompress(String path, boolean delete) throws Exception {
        File file = new File(path);
        String targetFilePath = "";
        decompress(file, delete, targetFilePath);
    }

    public static void main(String[] args) {
        String ep = "C:/opt/rgsc/upload/1478826911894.zip";
        String dp = "C:/opt/rgsc/yangc";
        File f = new File(dp);
        if (!f.exists()) {
            f.mkdirs();
        }
        //String p ="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANgOeh4EWUNiZPyAMZFV3SQO5D233JkxNMRbQmvRDCSlZJMVveGoraWNKa+Es3e18LTw3t+9/zhdhSd9NrMRF8gZbYuZVV+Ff/rqFg7VW5RdITO52dY0nQxxdZWeQuutxDwYROzuUWIeqk7md7nxCwM/oeSSa+giZy5aXAf2pT+ZAgMBAAECgYEAu6U/LEjMmfigCqjv4/D2AnYohu3j2uzgjlVQR8Gk0UMQKaqtYoqUXf9PIBCZqjPLXesBNsOWIQcgOV7HnhgPVPHeOMvTpPOjB4Mo6M1wv3eDSvurdW6cEikJFOuPvOcu94VDU9qOIcAIMyj0ECD+cA1htofeg80UJ0OxUl8hKIECQQD++7yZ3vagsY8HOTbvV/EiS8SZk/Sr1GuivOvrraApFSpH/iZNJ9CT+flAE4w/v1HsqxxDb33ZNOzRIiS4fEXpAkEA2OsB5/8ZD0zICFW+eMzwH9uriKSKvASwvfnB0w2Txd0r6GFAuz+mhJX0he98xPqtRcTfbZ1VHhGfzWX9tGwuMQJAf0GcCx9f7ysuuugvJpy5V+iAojVZczh8z6eCBwmMWXSauXt6Gh+41WF4qCYZZOdTN87eODm4kWL8nhNwSDKdoQJAKMLIYerqr5hTAlXya8q+2I2zxwJ8W2ZaO+ihxmIHwaeU557Wxhjo1oNDJqPhAzbDtpePEI7+/p57M+KVcQDfEQJBANBCFb29OfWXTRp01EVhjMnel4NoMHo+uvmWzwFgavrW7tUZQeohu7dDxX2wLmffW7wqiaDhLtkwRH6bhF+o5sw=";
        //RSAUtils.decodeFile(p, e, target);
        GZipUtils.decompress(ep, f.getAbsolutePath() + "/1478826911894", false);
    }
}